package com.pethub.controller.web;

import com.pethub.entity.User;
import com.pethub.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author harden
 * @version 1.0
 * @// TODO: 2017/12/3 0003  处理和用户相关的一起请求
 *
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private Logger logger=Logger.getLogger(UserController.class.getName());

    /**
     *
     * @param user,会按照和实体类的属性对应自动加入，对应方法为getParameter（）
     * @return 填充一个合适的map
     */
    @RequestMapping("/register")
    @ResponseBody
    public Map register(User user)
    {
        Map<String,Object> map=new LinkedHashMap<String, Object>();
        try {
            if(userService.getByEmail(user.getEmail())==null)
            {
                userService.save(user);
                map.put("result",1);
                logger.info(user.getEmail()+"注册成功！！！");
                map.put("id",user.getId());
            }else{
                map.put("result",0);
            }
        } catch (Exception e) {
            logger.error(user.getEmail()+"注册失败！！！----"+e.getMessage());
            map.put("result",0);
            e.printStackTrace();
        }
        return  map;
    }
}
